/**
 * Copyright (C), 2015-2019, bogewang
 * FileName: QQAuthoriServiceImpl
 * Author:   foren236598164@aliyun.com
 * Date:     2019/9/24 11:15
 * Description: QQ登录接口实现
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.member.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bogewang.core.base.BaseApiService;
import com.bogewang.core.base.BaseResponse;
import com.bogewang.core.constants.Constants;
import com.bogewang.core.core.token.TokenUtils;
import com.bogewang.member.entity.UserDTO;
import com.bogewang.member.mapper.UserMapper;
import com.bogewang.member.service.QQAuthoriService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 〈QQ登录接口实现〉
 *
 * @author foren236598164@aliyun.com
 * @create 2019/9/24
 * @since 1.0.0
 */
public class QQAuthoriServiceImpl extends BaseApiService<JSONObject> implements QQAuthoriService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private TokenUtils tokenUtils;

    @Override
    public BaseResponse<JSONObject> findTokenByOpenId(String qqOpenid) {
        // 1. 根据openId 查询用户信息;
        if (isBlank(qqOpenid)){
            return setResultError("qqOpenId 不能为空!");
        }

        // 2.查看是否有对应信息;
        UserDTO userDTO = userMapper.findByOpenId(qqOpenid);
        if (userDTO == null){
            return setResultError(Constants.HTTP_RES_CODE_203, "根据qqOpenId 没有查询到对应用户信息!");
        }

        // 3.返回对应token;
        String keyPrefix = Constants.MEMBER_TOKEN_KEYPREFIX + "QQ_LOGIN";
        Long userid = userDTO.getUserid();
        String userToken = tokenUtils.createToken(keyPrefix, userid + "");

        JSONObject data = new JSONObject();
        data.put("token", userToken);

        return setResultSuccess(data);
    }
}